package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constants.AvatarMessageConstants;
import com.qk.domain.business.BusDO;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusDTO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.UpdateBusinessDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @PutMapping
    public Result updateBusiness(@RequestBody UpdateBusinessDTO dto){
        businessService.updateBusiness(dto);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusinessInfoById(
            @PathVariable Integer id){
        BusinessVO vo = businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }
    @PostMapping("/toCustomer/{id}")
    public Result toBusiness(@PathVariable Integer id){
        businessService.toBusiness(id);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result poolList(BusDTO bDTO){
        log.info("接收到的查询参数: {}", bDTO);
        PageResult<BusDO> pageResult= businessService.poollist(bDTO);
        return Result.success(pageResult);
    }
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id){
        businessService.updateById(Business.builder()
                .id(id)
                .status(AvatarMessageConstants.RECOVERY)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateById(@PathVariable Integer businessId,
                             @PathVariable Integer userId
                             ){
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(AvatarMessageConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();

    }
    @PostMapping
    public Result add(@RequestBody Business business){
        businessService.add(business);
        return Result.success();
    }

    @GetMapping
    public Result list(BusinessDTO dto){
        PageResult<BusinessDO> pageResult= businessService.list(dto);
        return Result.success(pageResult);
    }
}